package waf

import (
	"mallgo/core"
	"mallgo/internal/common/response"
	"mallgo/internal/resources/waf/api/apis"
	"mallgo/internal/resources/waf/api/domain/request"

	"github.com/gin-gonic/gin"
)

type WafController struct {
	wafApi apis.WafApi
}

func NewWafController(wafApi apis.WafApi) *WafController {
	return &WafController{
		wafApi: wafApi,
	}
}

// CreateRule 创建规则
func (c *WafController) CreateRule(ctx *gin.Context) {
	var req request.RuleRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(200, response.Error(400, "参数错误"))
		return
	}

	res := c.wafApi.CreateRule(&req)
	ctx.JSON(200, res)
}

// GetRules 获取规则列表
func (c *WafController) GetRules(ctx *gin.Context) {
	ruleType := ctx.Query("type")
	res := c.wafApi.GetRules(ruleType)
	ctx.JSON(200, res)
}

// InitRouter 初始化路由
func InitRouter(wafController *WafController, s *core.AppServer) {
	api := s.Engine.Group("/api/waf")
	{
		api.POST("/rules", wafController.CreateRule)
		api.GET("/rules", wafController.GetRules)
	}
}
